#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.pith.packets.hashreader import HashFileReader, HashFileReaderError
from oriolus.pith.packets.hashwriter import HashFileWriter, Strategy, Visitor
from unittest import TestCase, TestLoader, TextTestRunner

class TestHashFileReader(TestCase):
    
    def setUp(self):        
        self.__name = "CHECKSUMS2"
        self.__checksum_file = os.path.join(os.getcwd(), self.__name)
        self.__strategy = Strategy() 
        self.__visitor = Visitor(os.getcwd())         

    def tearDown(self):        
        pass        
            
    def test_reader1(self):
        try:
            dict = self.__visitor.list(self.__strategy.get_file_hash)
            
            writer = HashFileWriter(self.__checksum_file)
            writer.write_strings(dict)
            
            reader = HashFileReader(self.__checksum_file)
            dict2 = reader.read()
            
            self.assertEqual(len(dict.keys()), len(dict2.keys()))        
            
            for key in dict.iterkeys():
                self.assertEqual(dict[key], dict2[key])
        finally:
            if os.path.exists(self.__checksum_file): os.remove(self.__checksum_file)
            
    def test_reader2(self):
        missing_file = os.path.join(os.getcwd(), "missing_file")
        reader = HashFileReader(missing_file)        
        
        self.assertRaises(HashFileReaderError, reader.read)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestHashFileReader)
    TextTestRunner(verbosity=2).run(suite)   
